package com.ynet.middleground.riskmanage.service.impl;

import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Throwables;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.riskmanage.bean.*;
import com.ynet.middleground.riskmanage.dto.PledgeOrderDTO;
import com.ynet.middleground.riskmanage.model.PledgeOrReliefPledgeOrderModel;
import com.ynet.middleground.riskmanage.service.PledgeOrReliefPledgeOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName PledgeOrReliefPledgeOrderServiceImpl
 * @Description 冷链及大港-质押监管与解除质押监管（静态/动态/换货）
 * @Author zhangyongbin
 * @Date 2020/8/19 17:09
 * @Version 1.0
 */
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Consumes({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Api(value = "冷链及大港-质押监管与解除质押监管（静态/动态/换货）", tags = {"冷链及大港-质押监管与解除质押监管（静态/动态/换货）"})
@Path("coldAndPort")
@Service(timeout = 6000, version = "0.0.1")
public class PledgeOrReliefPledgeOrderServiceImpl implements PledgeOrReliefPledgeOrderService, GenericService {

    @Autowired
    PledgeOrReliefPledgeOrderModel pledgeOrReliefPledgeOrderModel;

    @POST
    @Path("static")
    @ApiOperation(value = "质押监管与解除质押监管（静态）", notes = "质押监管与解除质押监管（静态）")
    @ApiImplicitParam(name = "requestObj", dataType = "StaticPledgeOrReliefPledgeReq", paramType = "body",
        required = true)
    @Override
    public ServiceResult<PledgeOrderDTO> staticPledgeOrRelief(@Valid StaticPledgeOrReliefPledgeReq req) {
        ServiceResult<PledgeOrderDTO> result = new ServiceResult();
        try {
            result.setResult(pledgeOrReliefPledgeOrderModel.staticPledgeOrRelief(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "质押监管与解除质押监管（静态）服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    @POST
    @Path("dynamic")
    @ApiOperation(value = "质押监管与解除质押监管（动态）", notes = "质押监管与解除质押监管（动态）")
    @ApiImplicitParam(name = "requestObj", dataType = "DynamicPledgeOrReliefPledgeReq", paramType = "body",
        required = true)
    @Override
    public ServiceResult<PledgeOrderDTO> dynamicPledgeOrRelief(@Valid DynamicPledgeOrReliefPledgeReq req) {
        ServiceResult<PledgeOrderDTO> result = new ServiceResult();
        try {
            result.setResult(pledgeOrReliefPledgeOrderModel.dynamicPledgeOrRelief(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "质押监管与解除质押监管（动态）服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    @POST
    @Path("change")
    @ApiOperation(value = "换货指令", notes = "换货指令")
    @ApiImplicitParam(name = "requestObj", dataType = "ChangeGoodsOrderReq", paramType = "body", required = true)
    @Override
    public ServiceResult<PledgeOrderDTO> changeGoodsOrder(@Valid ChangeGoodsOrderReq req) {
        ServiceResult<PledgeOrderDTO> result = new ServiceResult();
        try {
            result.setResult(pledgeOrReliefPledgeOrderModel.changeGoodsOrder(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "换货指令服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 静态质押/解除结果同步
     *
     * @param req 静态质押监管及解除结果同步信息
     * @return 以 success 字段为准，true 为成功，false 则为失败
     * @author liulx
     * @date 2020-08-18 16:53:41
     */
    @POST
    @Path("sync-static-result")
    @ApiOperation(value = "同步静态质押监管（解除）指令结果", notes = "怡之航冷链、大港系统可调用该服务，同步静态质押监管（解除）指令的执行结果")
    @ApiImplicitParam(name = "requestObj", dataType = "SyncPledgeStaticReq", paramType = "body", required = true)
    @Override
    public ServiceResult<Object> syncPledgeStatic(@Valid SyncPledgeStaticReq req) {
        ServiceResult<Object> serviceResult = new ServiceResult<>();
        try {
            serviceResult.setResult(pledgeOrReliefPledgeOrderModel.syncPledgeStatic(req));
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "同步静态质押监管（解除）指令结果服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            serviceResult.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return serviceResult;
    }

    /**
     * 动态质押/解除结果同步
     *
     * @param req 动态质押监管及解除结果同步信息
     * @return 以 success 字段为准，true 为成功，false 则为失败
     * @author liulx
     * @date 2020-08-19 14:58:35
     */
    @POST
    @Path("sync-dynamic-result")
    @ApiOperation(value = "同步动态质押监管（解除）指令结果", notes = "怡之航冷链、大港系统可调用该服务，同步动态质押监管（解除）指令的执行结果")
    @ApiImplicitParam(name = "requestObj", dataType = "SyncPledgeDynamicReq", paramType = "body", required = true)
    @Override
    public ServiceResult<Object> syncPledgeDynamic(@Valid SyncPledgeDynamicReq req) {
        ServiceResult<Object> serviceResult = new ServiceResult<>();
        try {
            serviceResult.setResult(pledgeOrReliefPledgeOrderModel.syncPledgeDynamic(req));
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "同步动态质押监管（解除）指令结果服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            serviceResult.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return serviceResult;
    }

    /**
     * 换货结果同步
     *
     * @param req 换货指令结果同步请求信息
     * @return 以 success 字段为准，true 为成功，false 则为失败
     * @author liulx
     * @date 2020-08-19 20:33:07
     */
    @POST
    @Path("sync-exchange-result")
    @ApiOperation(value = "同步换货指令结果", notes = "怡之航冷链、大港系统可调用该服务，同步换货指令结果的执行结果")
    @ApiImplicitParam(name = "requestObj", dataType = "SyncExchangeReq", paramType = "body", required = true)
    @Override
    public ServiceResult<Object> syncExchange(@Valid SyncExchangeReq req) {
        ServiceResult<Object> serviceResult = new ServiceResult<>();
        try {
            serviceResult.setResult(pledgeOrReliefPledgeOrderModel.syncExchange(req));
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "同步换货指令结果服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            serviceResult.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return serviceResult;
    }

}
